﻿/*----------------------------------------------------------------
// Copyright (C) 2012 苏富特健康科技
// 版权所有。 
//
// 文件名：MemoryCache.cs
// 文件功能描述：内存式缓存
//
// 
// 创建标识：2012-06-26 嵇俊良
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cache.Contract;
using System.Runtime.Caching;

namespace Cache.MemoryCache
{
    /// <summary>
    /// 内存缓存
    /// </summary>
    public class MemoryCache:ICache
    {
        ObjectCache cache ;
        public MemoryCache()
        {
            cache = System.Runtime.Caching.MemoryCache.Default;
        }

        /// <summary>
        /// 增加对象到缓存
        /// </summary>
        /// <typeparam name="T">可以序列化的对象</typeparam>
        /// <param name="key">Key</param>
        /// <param name="obj">对象</param>
        /// <returns>bool</returns>
        public bool Add<T>(string key, T obj)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.SlidingExpiration = TimeSpan.FromMinutes(60);

            try
            {
                cache.Set(key, obj, policy);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
           
        }

        /// <summary>
        /// 增加对象到缓存
        /// </summary>
        /// <typeparam name="T">可以序列化的对象</typeparam>
        /// <param name="key">Key</param>
        /// <param name="obj">对象</param>
        /// <param name="t">超时时间</param>
        /// <returns>bool</returns>
        public bool Add<T>(string key, T obj, TimeSpan t)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.SlidingExpiration = t;

            try
            {
                cache.Set(key, obj, policy);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 取得缓存中的对象
        /// </summary>
        /// <typeparam name="T">可以序列化的对象</typeparam>
        /// <param name="key">Key</param>
        /// <returns>T</returns>
        public T Get<T>(string key)
        {
            object o =cache.Get(key);
            if (o != null)
            {
                return (T)o;
            }
            else
            {
                return default(T);
            }
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>bool</returns>
        public bool Remove(string key)
        {
            cache.Remove(key);
            return true;
        }

        /// <summary>
        /// 清空所有缓存
        /// </summary>
        public void ClearAll()
        {
            throw new NotSupportedException("内存方式下不提供清空所有缓存");
        }
    }
}
